package org.misalen.web.server.controllers.driver;

import java.util.Date;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.utils.TextUtil;
import org.misalen.web.database.model.mysql.pro.ProDriverUser;
import org.misalen.web.database.model.mysql.pro.ProVideoInfo;
import org.misalen.web.database.model.mysql.pro.ProVideoReward;
import org.misalen.web.database.util.PaginationFrom;
import org.misalen.web.server.config.ErrorCode;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.driver.DriverVideoCompleteForm;
import org.misalen.web.server.forms.driver.DriverVideoDetailsForm;
import org.misalen.web.server.forms.driver.DriverVideoNewtaskForm;
import org.misalen.web.server.forms.driver.DriverVideoPlayForm;
import org.misalen.web.server.forms.driver.DriverVideoRedwarsForm;
import org.misalen.web.server.util.FormUtil;
import org.misalen.web.service.push.PushParameter;
import org.misalen.web.service.push.PushType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ControllerApi(value = "视频广告", description = "新任务、广告详情", index = 300)
@RequestMapping(value = ServerUrl.DriverVideoAdvertisingControllerUrl)
public class DriverVideoAdvertisingController extends BaseController {

	@RequestMapping(value = "/newtask", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "新任务", resultClass = { PaginationFrom.class, ProVideoInfo.class }, include = { "primaryKey",
			"videoName", "videoThumUrl", "redEnvelopTotal", "grabThe", "isGet", "getMoney" })
	public PaginationFrom<ProVideoInfo> newtask(@RequestBody DriverVideoNewtaskForm form) throws CustomerException {
		FormUtil.validate(form);
		PaginationFrom<ProVideoInfo> pagination = formToPage(form, ProVideoInfo.class);
		String userId = "1";
		String belongCity = null;
		if (form.getUserId() != null && !form.getUserId().equals("")) {
			ProDriverUser driverUser = proDriverUserRepository.get(ProDriverUser.class, form.getUserId());
			userId = driverUser.getPrimaryKey();
			belongCity = driverUser.getBelongCity();
		}
		return proVideoInfoRepository.findNewtaskList(form.getType(), belongCity, userId, pagination);
	}

	@RequestMapping(value = "/redwars", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "抢任务", include = { "money" }, resultClass = ProVideoReward.class)
	public @ResponseBody ServerResult redwars(@RequestBody DriverVideoRedwarsForm form) throws CustomerException {
		ProVideoInfo videoInfo = proVideoInfoRepository.getValid(form.getVideoId());
		if (videoInfo == null) {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10008);
		}
		
		long redwarsCount = proVideoRewardRepository.getValidCountByUser(form.getUserId(),videoInfo.getType());
		if (redwarsCount >= 10) {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10011);
		}
		
		long validCount = proVideoRewardRepository.getValidCountByVideo(form.getVideoId());
		if (validCount == 0) {
			proVideoInfoRepository.updateGrabThe(TextUtil.booleanToString(false), form.getVideoId());
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10008);
		}

		ProVideoReward proVideoReward = proVideoRewardRepository.getByVideoUser(form.getUserId(), form.getVideoId());
		if (proVideoReward != null) {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10009);
		}
		proVideoReward = proVideoRewardRepository.getValidByVideo(form.getVideoId());
		proDriverUserRepository.updateFreezeBalance(proVideoReward.getMoney(), form.getUserId());
		proVideoReward.setGetTime(new Date());
		proVideoReward.setUserId(form.getUserId());
		proVideoRewardRepository.update(proVideoReward);
		if (validCount == 1) {
			proVideoInfoRepository.updateGrabThe(TextUtil.booleanToString(true), form.getVideoId());
		}
		proVideoInfoRepository.updateRedEnvelopResidue(form.getVideoId());
		
		ProDriverUser proDriverUser = proDriverUserRepository.get(ProDriverUser.class, form.getUserId());
		String token;
		if (videoInfo.getType().equals("headrest")) {
			token = proDriverUser.getHeadrestCode();
		} else {
			token = proDriverUser.getBackseatCode();
		}
		jPushImpl.sendPush(new PushParameter()//
				.setNotification(false)//
				.setTitle("")//
				.setType(PushType.videoGrab.name())//
				.addToken(TextUtil.pustText(token)).addExtra("url", videoInfo.getVideoUrl())//
		);

		return ServerResult.ok(proVideoReward);
	}

	@RequestMapping(value = "/play", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "播放中", resultClass = { PaginationFrom.class, ProVideoInfo.class }, include = { "primaryKey",
			"videoName", "videoThumUrl", "redEnvelopTotal", "startDate", "stopDate", "getMoney", "playeTime", "playedTime" })
	public PaginationFrom<ProVideoInfo> play(@RequestBody DriverVideoPlayForm form) throws CustomerException {
		FormUtil.validate(form);
		PaginationFrom<ProVideoInfo> pagination = formToPage(form, ProVideoInfo.class);
		return proVideoInfoRepository.findListByDriverUser(form.getUserId(), form.getType(), pagination,
				TextUtil.booleanToString(true));
	}

	@RequestMapping(value = "/complete", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "已经完成", resultClass = { PaginationFrom.class, ProVideoInfo.class }, include = { "primaryKey",
			"videoName", "videoThumUrl", "startDate", "redEnvelopTotal", "stopDate", "getMoney", "playeTime", "playedTime" })
	public PaginationFrom<ProVideoInfo> complete(@RequestBody DriverVideoCompleteForm form) throws CustomerException {
		FormUtil.validate(form);
		PaginationFrom<ProVideoInfo> pagination = formToPage(form, ProVideoInfo.class);
		return proVideoInfoRepository.findListByDriverUser(form.getUserId(), form.getType(), pagination,
				TextUtil.booleanToString(false));
	}

	@RequestMapping(value = "/details", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "详情", resultClass = { PaginationFrom.class, ProVideoReward.class }, include = { "getTime",
			"money", "userName" })
	public PaginationFrom<ProVideoReward> details(@RequestBody DriverVideoDetailsForm form) throws CustomerException {
		FormUtil.validate(form);
		return proVideoRewardRepository.getListByGet(form.getVideoId());
	}
}
